import 'dart:convert';

import 'package:equatable/equatable.dart';

class Device extends Equatable {
  final int? id;
  final String? name;
  final String? thumbnailUrl;
  final int? channelCount;
  final Map<String, dynamic>? capabilities; // 仅设备详情有

  const Device({
    this.id,
    this.name,
    this.thumbnailUrl,
    this.channelCount,
    this.capabilities,
  });

  factory Device.fromMap(Map<String, dynamic> data) => Device(
        id: data['id'] as int?,
        name: data['name'] as String?,
        thumbnailUrl: data['thumbnailUrl'] as String?,
        channelCount: data['channelCount'] as int?,
      );

  // 从设备详情接口创建（含 capabilities）
  factory Device.fromDetailJson(Map<String, dynamic> json) {
    return Device(
      id: json['id'] as int?,
      name: json['name'] as String?,
      channelCount: json['channelCount'] as int?,
      thumbnailUrl: json['thumbnailUrl'] as String?,
      capabilities: json['capabilities'] != null
          ? json['capabilities'] as Map<String, dynamic>
          : null,
    );
  }
  Map<String, dynamic> toMap() => {
        'id': id,
        'name': name,
        'channelCount': channelCount,
        'thumbnailUrl': thumbnailUrl,
        'capabilities': capabilities,
      };

  /// `dart:convert`
  ///
  /// Parses the string and returns the resulting Json object as [Device].
  factory Device.fromJson(String data) {
    return Device.fromMap(json.decode(data) as Map<String, dynamic>);
  }

  /// `dart:convert`
  ///
  /// Converts [Device] to a JSON string.
  String toJson() => json.encode(toMap());

  Device copyWith(
      {int? id,
      String? name,
      int? channelCount,
      String? thumbnailUrl,
      Map<String, dynamic>? capabilities}) {
    return Device(
      id: id ?? this.id,
      name: name ?? this.name,
      thumbnailUrl: thumbnailUrl ?? this.thumbnailUrl,
      channelCount: channelCount ?? this.channelCount,
      capabilities: capabilities ?? this.capabilities,
    );
  }

  @override
  bool get stringify => true;

  @override
  List<Object?> get props => [id, name, channelCount, capabilities];
}
